<?php

require 'FActionIcon.class.php';

class FMainContainer extends FContainer
{
    
    private $lastX = 20;
    private $lastY = 20;
    private $iconCount = 0;
    private $numIconsColumn = 0;
    
    public function __construct()
    {
        parent::__construct('mainContainer');
        $this->setLayout(FLayout::ABSOLUTE_LAYOUT);
        $this->setLayoutConfig(array(
            'columns' => round(FUtil::getScreenWidth() / 100)));
        $this->setBaseCls('desktop');
        $this->setPadding(10);
    }
    
    public function addIcon($text, $image, $action)
    {
        $isFirst = count($this->getItems()) == 0;
        
        $icon = new FActionIcon('icon_' . rand(), $text, $action, $image);
        
        $x = $this->getLastX();
        $y = $this->getLastY();
        
        $firstOfTheColumn = $this->getIconCount() % (round(FUtil::getScreenHeight() / 100) - 1) == 0;
        
        if ( ($this->getIconCount() > 0 && $firstOfTheColumn) || ($this->getNumIconsColumn() > 0 && $this->getIconCount() >= $this->getNumIconsColumn()) )
        {
            if ( $this->getNumIconsColumn() == 0 )
            {
                $this->setNumIconsColumn(count($this->getItems()) - 1);
            }
            
            $x = $x + 100;
            $y = 20;
            
            $this->setLastX($x);
            $this->setLastY($y);
            
            $icon->setX($x);
            $icon->setY($y);
            $this->iconCount = 0;
        }
        else
        {
            $icon->setX($x);
            
            if ( !$isFirst && $y == 20 )
            {
                $y = $y + 100;
            }
            $icon->setY($y);
            
            $y = $y + 100;
            $this->setLastY($y);
            $this->iconCount++;
        }
                        
        $this->addItem($icon);
    }
    
    public function getLastX() {
        return $this->lastX;
    }

    public function setLastX($lastX) {
        $this->lastX = $lastX;
    }

    public function getLastY() {
        return $this->lastY;
    }

    public function setLastY($lastY) {
        $this->lastY = $lastY;
    }
    
    public function getIconCount() {
        return $this->iconCount;
    }

    public function setIconCount($iconCount) {
        $this->iconCount = $iconCount;
    }
    
    public function getNumIconsColumn() {
        return $this->numIconsColumn;
    }

    public function setNumIconsColumn($numIconsColumn) {
        $this->numIconsColumn = $numIconsColumn;
    }
    
}

?>
